/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.e.ego.widget.decorator

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.support.v4.view.ViewCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.e.ego.util.EDensity

/**
 * RecyclerView线性分割线
 */
class EDividerDecoration @JvmOverloads constructor(private val mContext: Context, orientation: Int = LinearLayoutManager.VERTICAL) : RecyclerView.ItemDecoration() {

    val ATTRS = intArrayOf(android.R.attr.listDivider)

    val HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL

    val VERTICAL_LIST = LinearLayoutManager.VERTICAL

    private var mDivider: Drawable? = null

    private var mOrientation: Int = 0

    private var mLeftPadding = 0
    private var mRightPadding = 0

    private var mTopPadding = 0
    private var mBottomPadding = 0

    init {
        setDefaultDivider()
        setOrientation(orientation)
    }

    fun setOrientation(orientation: Int) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw IllegalArgumentException("invalid orientation")
        }
        mOrientation = orientation
    }

    fun setHorizontalOrientation(): EDividerDecoration {
        setOrientation(HORIZONTAL_LIST)
        return this
    }

    fun setVerticalOrientation(): EDividerDecoration {
        setOrientation(VERTICAL_LIST)
        return this
    }

    fun setLeftPadding(leftPadding: Int): EDividerDecoration {
        this.mLeftPadding = EDensity.dip2px(mContext,leftPadding)
        return this
    }

    fun setRightPadding(rightPadding: Int): EDividerDecoration {
        this.mRightPadding = EDensity.dip2px(mContext,rightPadding)
        return this
    }

    fun setTopPadding(topPadding: Int): EDividerDecoration {
        this.mTopPadding = EDensity.dip2px(mContext,topPadding)
        return this
    }

    fun setBottomPadding(bottomPadding: Int): EDividerDecoration {
        this.mBottomPadding = EDensity.dip2px(mContext,bottomPadding)
        return this
    }

    fun setDivider(drawableRes: Int): EDividerDecoration {
        mDivider = mContext.resources.getDrawable(drawableRes)
        return this
    }

    fun setDivider(divider: Drawable): EDividerDecoration {
        this.mDivider = divider
        return this
    }

    fun setDivider(attrs: IntArray) {
        val a = mContext.obtainStyledAttributes(attrs)
        mDivider = a.getDrawable(0)
        a.recycle()
    }

    fun setDefaultDivider() {
        setDivider(ATTRS)
    }

    override fun onDraw(c: Canvas, parent: RecyclerView) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent)
        } else {
            drawHorizontal(c, parent)
        }
    }

    fun drawVertical(c: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft + mLeftPadding
        val right = parent.width - parent.paddingRight - mRightPadding

        val childCount = parent.childCount
        for (i in 0..childCount - 1) {
            val child = parent.getChildAt(i)
            val params = child
                    .layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin +
                    Math.round(ViewCompat.getTranslationY(child))
            val bottom = top + mDivider!!.intrinsicHeight
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(c)
        }
    }

    fun drawHorizontal(c: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop + mTopPadding
        val bottom = parent.height - parent.paddingBottom - mBottomPadding

        val childCount = parent.childCount
        for (i in 0..childCount - 1) {
            val child = parent.getChildAt(i)
            val params = child
                    .layoutParams as RecyclerView.LayoutParams
            val left = child.right + params.rightMargin +
                    Math.round(ViewCompat.getTranslationX(child))
            val right = left + mDivider!!.intrinsicHeight
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider!!.draw(c)
        }
    }

    override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView?) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider!!.intrinsicHeight)
        } else {
            outRect.set(0, 0, mDivider!!.intrinsicWidth, 0)
        }
    }
}
